package com.rabbitmq.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestRabbitmqController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/send")
    public String send() {
        String msg = "hello";
        rabbitTemplate.convertAndSend("myDirectExchange", "my.direct.routing", msg);
        return "success";
    }

    /**
     * workQueue
     * 向队列中不停发送消息，模拟消息堆积。
     * publisher服务中的SpringAmqpTest类中添加一个测试方法
     */
    @RequestMapping("/testWorkQueue")
    public void testWorkQueue() throws InterruptedException {
        // 队列名称
        String queueName = "myWorkQueues";
        // 消息
        String message = "hello, message_";
        for (int i = 0; i < 50; i++) {
            //发送消息
            rabbitTemplate.convertAndSend(queueName, message + i);
        }
    }

    //交换机类型测试
    @RequestMapping("/testExchanges")
    public void testExchanges() throws InterruptedException {
        //Fanout交换机  广播模式
        // 因为在DirectRabbitConfig已经绑定交换机,这里我们不需要在次指定
        //直接使用队列名字即可
        //临时队列
        rabbitTemplate.convertAndSend("testExchangesQueues","",
                "测试Fanout交换机");
        rabbitTemplate.convertAndSend("testExchangesQueues","",
                "测试Fanout交换机");
    }


}
